package com.haircut.scheduled;


import cn.hutool.core.date.DateUtil;
import com.haircut.project.service.finance.IDeptCalcService;
import com.haircut.project.service.finance.IStaticFinanceService;
import com.haircut.project.service.salary.ISalaryCalcService;
import com.haircut.project.service.salary.ISalaryService;
import com.modular.redis.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class RecordScheduledService {

    @Autowired
    private ISalaryCalcService salaryCalcService;
    @Autowired
    private IStaticFinanceService staticFinanceService;
    @Autowired
    private IDeptCalcService deptCalcService;
    @Autowired
    @Qualifier("salaryExecutor")
    private AsyncTaskExecutor asyncTaskExecutor;
    @Autowired
    private RedisUtils redisUtils;
    private final Logger logger = LoggerFactory.getLogger(RecordScheduledService.class);

    /**
     * 计算工资，每天计算一次
     */
    @Scheduled(cron = "0 2 1,2,3,4 * * ?")
    public void calcSalaryScheduled() {
        logger.info("calcSalaryScheduled date {}", DateUtil.formatDateTime(DateUtil.date()));
        String reidsKey  = "sale:batch:cacle";
        if(redisUtils.checkRedisKey(reidsKey)){
            return;
        }
        // 刷新缓存
        redisUtils.flushRedis(reidsKey);
        asyncTaskExecutor.execute(()->{
            try {
                salaryCalcService.calcSalary(reidsKey,Boolean.FALSE,DateUtil.yesterday());
            }catch (Exception e){
                logger.error("calcSalaryScheduled err ",e);
            }finally {
                redisUtils.delKey(reidsKey);
            }
        });

    }

    /**
     * 计算部门数据，每20分钟执行异常
     */
    @Scheduled(cron = "0 0/20 * * * ?")
    public void staticCurrDay() {
        logger.info("staticCurrDay date {}", DateUtil.formatDateTime(DateUtil.date()));
        String reidsKey  = "finance:batch:curr";
        if(redisUtils.checkRedisKey(reidsKey)){
            return;
        }
        // 刷新缓存
        redisUtils.flushRedis(reidsKey);
        asyncTaskExecutor.execute(()->{
            try {
                staticFinanceService.staticCurrDay(reidsKey);
            }catch (Exception e){
                logger.error("staticCurrDay err ",e);
            }finally {
                redisUtils.delKey(reidsKey);
            }
        });

    }

    /**
     * 计算本月的统计数据
     * 每天 1，2，3五个小时 5分执行
     */
    @Scheduled(cron = "0 5 1,2,3 * * ?")
    public void staticWeekDay() {
        logger.info("staticWeekDay date {}", DateUtil.formatDateTime(DateUtil.date()));
        String reidsKey  = "finance:batch:week";
        if(redisUtils.checkRedisKey(reidsKey)){
            return;
        }
        // 刷新缓存
        redisUtils.flushRedis(reidsKey);
        asyncTaskExecutor.execute(()->{
            try {
                staticFinanceService.staticWeekDay(reidsKey);
            }catch (Exception e){
                logger.error("staticMonthDay err ",e);
            }finally {
                redisUtils.delKey(reidsKey);
            }
        });

    }

    /**
     * 每天 1，2，3五个小时 5分执行
     */
    @Scheduled(cron = "0 5 1,2,3,4 * * ?")
    public void calcDeptFinance() {
        logger.info("staticCurrDay date {}", DateUtil.formatDateTime(DateUtil.date()));
        String reidsKey  = "finance:detp:calc";
        if(redisUtils.checkRedisKey(reidsKey)){
            return;
        }
        // 刷新缓存
        redisUtils.flushRedis(reidsKey);
        asyncTaskExecutor.execute(()->{
            try {
                deptCalcService.calcDeptFinance(reidsKey);
            }catch (Exception e){
                logger.error("staticCurrDay err ",e);
            }finally {
                redisUtils.delKey(reidsKey);
            }
        });

    }
}
